//
//  GLFormRow.m
//  KTools
//
//  Created by 121 on 2017/10/24.
//  Copyright © 2017年 杭州吉利控股信息中心. All rights reserved.
//

#import "GLFormRow.h"
#import "GLFormBaseCell.h"
#import "GLCustomCell.h"
#import <objc/runtime.h>
#import "GLFormViewController.h"

@interface GLFormRow()
@property(nonatomic,strong)NSArray* componentKeys;

@property(nonatomic,strong)NSMutableDictionary* cellConfigDict;
@property(nonatomic,strong)NSMutableDictionary* cellDefaultDict;
@property(nonatomic,strong)id dataForCustomCell;

@end;

@implementation GLFormRow
@synthesize value=_value;




+(instancetype)formRowByRegisterClass:(Class)cellClass withTag:(NSString*)tag{
    GLFormRow* row = [[GLFormRow alloc]initWithTag:tag cellClass:cellClass];
    return row;
}

-(instancetype)initWithTag:(NSString*)tag cellClass:(Class)cellClass{
    NSAssert(tag, @"tag can't been nil");

    if (self = [super init]) {
        _tag = tag;
        _rowHeight = 44.0f;
        _isRequried = NO;
        _cellClass = cellClass;
    }
    return self;
}

-(GLFormBaseCell*)cellForFormTableView:(UITableView*)tableView{
    GLFormBaseCell* cell = [tableView dequeueReusableCellWithIdentifier:self.tag];
    if (!cell) {
        if(self.cellClass){
            if ([self.cellClass isEqual:[GLCustomCell class]]) {
                if (self.componentKeys) {
                    cell = [[GLCustomCell alloc]initWithCustom:self.componentKeys reuseIdentifier:self.tag];
                }else{
                    cell = [[GLCustomCell alloc]initWithIdentifier:self.tag];
                }
                
                ((GLCustomCell*)cell).valueKey = self.valueKey;
            }else{
                cell = [[self.cellClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.tag];
            }
            
            if (self.cellDefaultDict) {
                for (NSString* key in self.cellDefaultDict) {
                    [cell setValue:self.cellDefaultDict[key] forKeyPath:key];
                }
            }
        }
    }

    if (self.cellConfigDict) {
        for (NSString* keyPath in self.cellConfigDict) {
            [cell setValue:self.cellConfigDict[keyPath] forKeyPath:keyPath];
        }
    }
    if (self.dataForCustomCell && [self.cellClass isEqual:[GLCustomCell class]]) {
        [((GLCustomCell*)cell) updateCellByModel:self.dataForCustomCell];
    }

    [cell setRowValue:self.value];
    cell.isRequired = self.isRequried;

    cell.formRow = self;
    self.cell = cell;
    return cell;
}



/**
 仅在初始化时调用一次
 [cell setValue:value forKey:key]
 */
-(void)addDefaultValue:(id)value forKeyPath:(NSString*)key{
    if (!self.cellDefaultDict) {
        self.cellDefaultDict = [NSMutableDictionary dictionaryWithCapacity:0];
    }
    if ([self.cellDefaultDict isMemberOfClass:[NSDictionary class]]) {
        self.cellDefaultDict = [self.cellDefaultDict mutableCopy];
    }
    [self.cellDefaultDict setValue:value forKey:key];
}

/**
 初始化及复用时，均调用
 [cell setValue:value forKey:key]
 */
-(void)addConfigValue:(id)value forKeyPath:(NSString*)key{
    if (!self.cellConfigDict) {
        self.cellConfigDict = [NSMutableDictionary dictionaryWithCapacity:0];
    }
    if ([self.cellConfigDict isMemberOfClass:[NSDictionary class]]) {
        self.cellConfigDict = [self.cellConfigDict mutableCopy];
    }
    [self.cellConfigDict setValue:value forKey:key];
}


/**
 仅在初始化时调用一次
 [cell setValue:value forKey:key]
 */

-(void)addDefaultValuesByDictionary:(NSDictionary*)dict{
    if (!self.cellDefaultDict) {
        self.cellDefaultDict = [NSMutableDictionary dictionaryWithCapacity:0];
    }
    if ([self.cellDefaultDict isMemberOfClass:[NSDictionary class]]) {
        self.cellDefaultDict = [self.cellDefaultDict mutableCopy];
    }
    [self.cellDefaultDict setValuesForKeysWithDictionary:dict];
}


/**
 初始化及复用时，均调用
 [cell setValue:value forKey:key]
 */
-(void)addConfigValuesByDictionary:(NSDictionary*)dict{
    if (!self.cellConfigDict) {
        self.cellConfigDict = [NSMutableDictionary dictionaryWithCapacity:0];
    }
    if ([self.cellConfigDict isMemberOfClass:[NSDictionary class]]) {
        self.cellConfigDict = [self.cellConfigDict mutableCopy];
    }
    [self.cellConfigDict setValuesForKeysWithDictionary:dict];
}




-(void)setRowValue:(id)newValue{
    self.value = newValue;
    if (self.cell) {
        [self.cell setRowValue:newValue];
    }
}


@end

static char staticGLFormRowValueKey;

@implementation GLFormRow (GLCustomCell)

+(instancetype)formRowByComponentKeys:(NSArray*)componentKeys withTag:(NSString*)tag{
    GLFormRow* row = [[GLFormRow alloc]initWithTag:tag cellClass:[GLCustomCell class]];
    row.componentKeys = componentKeys;
    return row;
}

+(instancetype)formRowWithCustomCellIdentifier:(NSString*)tag {
    GLFormRow* row = [[GLFormRow alloc]initWithTag:tag cellClass:[GLCustomCell class]];
    return row;
}

- (void)updateCellByModel:(id)obj{
    self.dataForCustomCell = obj;
}

//for GLCustomCell
-(void)addConfigValue:(id)value componentKey:(NSString*)ckey forKeyPath:(NSString*)keyPath{
    NSAssert(value,@"addConfigValue error,value can't been nil");
    NSAssert(ckey,@"addConfigValue error,ckey can't been nil");
    
    if (keyPath) {
        ckey = [ckey stringByAppendingFormat:@".%@",keyPath];
    }
    [self addConfigValue:value forKeyPath:ckey];
}

//for GLCustomCell
-(void)addDefaultValue:(id)value componentKey:(NSString*)ckey forKeyPath:(NSString*)keyPath{
    NSAssert(value,@"addDefaultValue error,value can't been nil");
    NSAssert(ckey,@"addDefaultValue error,ckey can't been nil");
    
    if (keyPath) {
        ckey = [ckey stringByAppendingFormat:@".%@",keyPath];
    }
    [self addDefaultValue:value forKeyPath:ckey];
}

#pragma mark- setter and getter
-(void)setValueKey:(NSString *)valueKey{
    objc_setAssociatedObject(self, &staticGLFormRowValueKey, valueKey, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSString*)valueKey{
    return  objc_getAssociatedObject(self, &staticGLFormRowValueKey);
}
//-(GLBadgeLabel*)badgeLabel{
//    return  objc_getAssociatedObject(self, &GLBadgeLabelLKey);
//}
//
//-(void)setBadgeLabel:(GLBadgeLabel *)badgeLabel{
//    GLBadgeLabel* label = [[GLBadgeLabel alloc]init];
//    label.font = [UIFont systemFontOfSize:12];
//    label.backgroundColor = [UIColor redColor];
//    [self addSubview:label];
//    objc_setAssociatedObject(self, &GLBadgeLabelLKey, label, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
//}


@end
